/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "GameTimer.h"
#include <windows.h>
#include "../Math/Math.h"
namespace Hogshead
{
	namespace Common
	{
		GameTimer::GameTimer(void)
		: _seconds_per_count(0.0), _update_delta(0.0), _draw_delta(0.0), _start_time(0), _previous_update_time(0), _current_update_time(0), _previous_draw_time(0), _current_draw_time(0)
		{
			_update_frame_count = 0;
			memset(_update_frame_time_buffer, 0, sizeof(float) * 100);
			_running_update_total = 0.0f;

			//Get the inverse of the performance frequency to be used for time calculations.
			//this does not change once the program begins which allows for accurate
			//time calcualtion.
			__int64 counts_per_second;
			QueryPerformanceFrequency((LARGE_INTEGER*)&counts_per_second);
			_seconds_per_count = 1.0 / (double)counts_per_second;
			_times_handle = 0;
			_paused = true;
			_is_draw_cycle = true;
		}

		GameTimer::~GameTimer(void)
		{
		}

		void GameTimer::pause()
		{
			_paused = true;
		}

		void GameTimer::unPause()
		{
			_paused = false;
			updateTick();
			updateTick();
			drawTick();
			drawTick();
		}

		float GameTimer::getGameTime()const
		{
			return (float) ((_current_update_time - _paused_time - _start_time)*_seconds_per_count);
		}

		float GameTimer::getUpdateDelta()const
		{
			return (float)_update_delta;
		}
		
		float GameTimer::getDrawDelta()const
		{
			return (float)_draw_delta;
		}

		float GameTimer::getFPS()const
		{
			//return  _times_count/(float)( ( _recent_base_times[_times_handle] - _recent_base_times[(_times_handle + 1)%_times_count] )*_seconds_per_count);
			float mean_frame_time = _running_update_total / Math::min(100.0f, (float)_update_frame_count);
			return 1.0f / mean_frame_time;
		}

		//Used to capture a snapshot of what time is is now and reset timing values.
		void GameTimer::startTimer()
		{
			__int64 current_time;

			QueryPerformanceCounter((LARGE_INTEGER*)&current_time);

			_start_time = current_time;
			_previous_update_time = current_time;
			_previous_draw_time = current_time;			

			_paused_time = 0;

			_update_delta = 0;
			_draw_delta = 0;

			_paused = false;
		}

		//Used to keep track of time based on cycle system.
		void GameTimer::updateTick()
		{
			//grab the current time this cycle
			__int64 current_time;
			QueryPerformanceCounter((LARGE_INTEGER*)&current_time);

			if(_paused)
			{
				_paused_time += current_time-_current_update_time;
			}
			else
			{
				_current_update_time = current_time;

				//calculate the change in time
				_update_delta = (_current_update_time - _previous_update_time) * _seconds_per_count;

				_update_frame_count++;
				_running_update_total -= _update_frame_time_buffer[_update_frame_count % 100];
				_running_update_total += (float)_update_delta;
				_update_frame_time_buffer[_update_frame_count % 100] = (float)_update_delta;


				//save off time for next frame
				_previous_update_time = _current_update_time;

				//crazy things can happen so we need to be on top of it.
				if(_update_delta < 0.0)
				{
					_update_delta = 0.0;
				}		
			}
	
		}

		void GameTimer::drawTick()
		{
			//grab the current time this cycle
			__int64 current_time;
			QueryPerformanceCounter((LARGE_INTEGER*)&current_time);

			if(_paused)
			{
				
			}
			else
			{
				_current_draw_time = current_time;

				//calculate the change in time
				_draw_delta = (_current_draw_time - _previous_draw_time) * _seconds_per_count;

				//save off time for next frame
				_previous_draw_time = _current_draw_time;

				//crazy things can happen so we need to be on top of it.
				if(_draw_delta < 0.0)
				{
					_draw_delta = 0.0;
				}

				//save this tick's time off
				_times_handle++;
				_times_handle %= _times_count;

				_recent_base_times[_times_handle] = _current_draw_time;
			}
		}

		bool GameTimer::isDrawCycle() const
		{
			return _is_draw_cycle;
		}

		void GameTimer::setIsDrawCycle(bool is_draw_cycle)
		{
			_is_draw_cycle = is_draw_cycle;
		}
	}
}